<template>
  <div class="account-container">
    <div class="page-header">
      <div class="page-title">账号密码管理</div>
      <el-button type="primary" @click="handleAdd">添加账号</el-button>
    </div>

    <!-- 搜索表单 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="标题">
        <el-input v-model="searchForm.title" placeholder="请输入标题" clearable />
      </el-form-item>
      <el-form-item label="账号">
        <el-input v-model="searchForm.username" placeholder="请输入账号" clearable />
      </el-form-item>
      <el-form-item label="分类">
        <el-input v-model="searchForm.category" placeholder="请输入分类" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="accountList" border class="table-container">
      <el-table-column prop="title" label="标题" width="180" />
      <el-table-column prop="username" label="账号" width="180" />
      <el-table-column prop="url" label="网址" show-overflow-tooltip />
      <el-table-column prop="category" label="分类" width="100" />
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="success" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="pagination.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="pagination.pageSize"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px" @closed="resetForm">
      <el-form ref="accountForm" :model="accountForm" :rules="accountRules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="accountForm.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="accountForm.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="accountForm.password" placeholder="请输入密码" show-password>
            <el-button slot="append" @click="generateRandomPassword">生成</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="网址">
          <el-input v-model="accountForm.url" placeholder="请输入网址" />
        </el-form-item>
        <el-form-item label="分类">
          <el-input v-model="accountForm.category" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="accountForm.remark" type="textarea" :rows="3" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog title="账号详情" :visible.sync="viewDialogVisible" width="500px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="标题">{{ accountDetail.title }}</el-descriptions-item>
        <el-descriptions-item label="账号">{{ accountDetail.username }}</el-descriptions-item>
        <el-descriptions-item label="密码">
          <span v-if="showPassword">{{ accountDetail.password }}</span>
          <span v-else>********</span>
          <el-button size="mini" type="text" @click="togglePasswordVisibility">
            {{ showPassword ? '隐藏' : '显示' }}
          </el-button>
          <el-button size="mini" type="text" @click="copyPassword">复制</el-button>
        </el-descriptions-item>
        <el-descriptions-item label="网址">
          <a v-if="accountDetail.url" :href="accountDetail.url" target="_blank">{{ accountDetail.url }}</a>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="分类">{{ accountDetail.category || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ accountDetail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getAccountList, addAccount, updateAccount, deleteAccount, getAccountDetail, generatePassword } from '@/api/account'

export default {
  name: 'Account',
  data() {
    return {
      // 搜索表单
      searchForm: {
        title: '',
        username: '',
        category: ''
      },
      // 分页
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 账号列表
      accountList: [],
      // 加载状态
      loading: false,
      // 对话框
      dialogVisible: false,
      dialogTitle: '添加账号',
      submitLoading: false,
      // 表单
      accountForm: {
        id: null,
        title: '',
        username: '',
        password: '',
        url: '',
        category: '',
        remark: ''
      },
      // 表单校验规则
      accountRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      // 查看对话框
      viewDialogVisible: false,
      accountDetail: {},
      showPassword: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取账号列表
    fetchData() {
      this.loading = true
      getAccountList({
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
        title: this.searchForm.title,
        username: this.searchForm.username,
        category: this.searchForm.category
      })
        .then(response => {
          const { data } = response
          this.accountList = data.records
          this.pagination.total = data.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 搜索
    handleSearch() {
      this.pagination.pageNum = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        title: '',
        username: '',
        category: ''
      }
      this.handleSearch()
    },
    // 分页大小变化
    handleSizeChange() {
      this.fetchData()
    },
    // 页码变化
    handleCurrentChange() {
      this.fetchData()
    },
    // 添加账号
    handleAdd() {
      this.dialogTitle = '添加账号'
      this.accountForm = {
        id: null,
        title: '',
        username: '',
        password: '',
        url: '',
        category: '',
        remark: ''
      }
      this.dialogVisible = true
    },
    // 查看账号
    handleView(row) {
      this.viewDialogVisible = true
      this.showPassword = false
      getAccountDetail(row.id)
        .then(response => {
          this.accountDetail = response.data
        })
    },
    // 编辑账号
    handleEdit(row) {
      this.dialogTitle = '编辑账号'
      getAccountDetail(row.id)
        .then(response => {
          this.accountForm = { ...response.data }
          this.dialogVisible = true
        })
    },
    // 删除账号
    handleDelete(row) {
      this.$confirm('确认删除该账号吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteAccount(row.id)
            .then(() => {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.fetchData()
            })
        })
        .catch(() => {})
    },
    // 提交表单
    submitForm() {
      this.$refs.accountForm.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.accountForm.id) {
            // 更新
            updateAccount(this.accountForm)
              .then(() => {
                this.$message({
                  type: 'success',
                  message: '更新成功'
                })
                this.dialogVisible = false
                this.fetchData()
                this.submitLoading = false
              })
              .catch(() => {
                this.submitLoading = false
              })
          } else {
            // 添加
            addAccount(this.accountForm)
              .then(() => {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.dialogVisible = false
                this.fetchData()
                this.submitLoading = false
              })
              .catch(() => {
                this.submitLoading = false
              })
          }
        }
      })
    },
    // 重置表单
    resetForm() {
      if (this.$refs.accountForm) {
        this.$refs.accountForm.resetFields()
      }
    },
    // 生成随机密码
    generateRandomPassword() {
      generatePassword()
        .then(response => {
          this.accountForm.password = response.data
        })
    },
    // 切换密码可见性
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword
    },
    // 复制密码
    copyPassword() {
      const textarea = document.createElement('textarea')
      textarea.value = this.accountDetail.password
      document.body.appendChild(textarea)
      textarea.select()
      document.execCommand('copy')
      document.body.removeChild(textarea)
      this.$message({
        message: '密码已复制到剪贴板',
        type: 'success'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.account-container {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }
}
</style> 